package cn.fufeiii.bt.common.util.result;

import cn.fufeiii.bt.common.code.CommonResultEnum;
import lombok.Data;

/**
 * 通用响应结果
 *
 * @author FuFei
 */
@Data
public final class CommonResult<T> {

    private final boolean success;
    private final Integer code;
    private final String message;
    private final T data;

    private CommonResult(Integer code, String message, T data) {
        this.success = CommonResultEnum.SERVER_SUCCESS.getCode().equals(code);
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> CommonResult<T> success() {
        return success(null);
    }

    public static <T> CommonResult<T> success(T data) {
        return new CommonResult<>(CommonResultEnum.SERVER_SUCCESS.getCode(), CommonResultEnum.SERVER_SUCCESS.getMessage(), data);
    }

    public static <T> CommonResult<T> success(CommonResultEnum ee, T data) {
        return new CommonResult<>(ee.getCode(), ee.getMessage(), data);
    }

    public static CommonResult<Void> fail(CommonResultEnum ee) {
        return new CommonResult<>(ee.getCode(), ee.getMessage(), null);
    }

    public static CommonResult<Void> fail(Integer code, String msg) {
        return new CommonResult<>(code, msg, null);
    }

}